package com.cow.sram.module.authority;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cow.framework.core.exception.BusinessException;
import com.cow.framework.utils.reflection.MapToBeanUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.comm.config.constant.MessageConstants;
import com.cow.sram.data.entity.Roleinfo;
import com.cow.sram.module.distribution.RoleResourceService;
import com.cow.sram.module.distribution.UserroleService;
import com.cow.support.dto.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

/**
 * 角色控制类
 * @author Administrator
 *
 */

@RestController
@EnableAutoConfiguration
@Transactional
@RequestMapping(value="/authority")
public class RoleInfoController extends BaseComponent{
	@Autowired
	RoleInfoService roleService;
	@Autowired
	RoleResourceService roleResourceService;
	@Autowired
	UserroleService UserroleService;
	
	/**列表显示*/
	@RequestMapping(value="/queryRoleinfo")
	public Object queryRoleinfo(@RequestBody Map parmMap){
		 Page<Map> list = roleService.findByRoleName(parmMap);
		return new PageInfo<Map>(list);
//		 return list;
		
	}
	
	/**根据id查看角色*/
	@RequestMapping(value="/getRoleById")
	public Object getRoleById(@RequestParam Integer id){
		Roleinfo roleinfo=roleService.getRoleinfoById(id);
		return roleinfo;
	}
	
	/**新增角色*/
	@RequestMapping(value="/newRoleinfo")
	public Object newRoleinfo(@RequestBody Roleinfo roleinfo){
		roleinfo.setGentime(new Date());
		roleinfo.setCreator(super.getCurUserId());
		roleService.saveRoleinfo(roleinfo);
		return "success";
		
	}
	
	/**修改角色*/
	@RequestMapping(value="/editRoleinfo")
	public Object editRoleinfo(@RequestBody Map parmMap){
		//根据id查询角色
		Roleinfo roleinfo=roleService.getRoleinfoById(Integer.valueOf(parmMap.get("id").toString()));
		MapToBeanUtil.copyMapToBean(parmMap, roleinfo);//将map拷贝到实体
		//保存更新
		roleService.saveRoleinfo(roleinfo);
		return "success";
	}
	
	/**删除角色*/
	@RequestMapping(value="/delRoleinfo")
	public Object delRoleinfo(@RequestParam Integer id){
//		List<User> userList = super.getAuthClient().getUserByRoleId(id.longValue());
//		if(userList != null && !userList.isEmpty()){
//			throw new BusinessException("B1017",MessageConstants.B1017);
//		}
		UserroleService.delRoleId(id);
		roleResourceService.delroleId(id);
		roleService.delRoleinfo(id);
		return "success";
	}
	
	
}
